﻿function OnShowData(submitButtonId) {
    $("#ReportViewPlace").html('<div style="width:100%; heigt:100%; text-align:center; vertical-align: middle;"><span class="blink_text">Report are processing data...<br/><span id="countdowntime"></span></span><br /><img id="img1" src="../../Content/images/ajax-loading.gif" alt="loading" /></div>');
    $('#ReportViewPlace').dialog('open');    
    $(".blink_text").cyclicFade({
        repeat: 0,
        params: [
				    { fadeout: 2000, stayout: 200, opout: 0.25, fadein: 2000, stayin: 400, opin: 1.0 }
			    ]
    });
    $('#countdowntime').countdown({
        since: new Date(),
        format: 'HMS',
        compact: true
    });
    
    $("#" + submitButtonId).click();
}

function SetDateRange(text) {
    var curDate = new Date();
    $("#dateFrom, #dateTo").datepicker('disable');
    $('#monthSliderContainer').hide();
    switch (text) {
        case 'This Week':
            $('#dateFrom').val($.datepicker.formatDate('dd/mm/yy', Date.parse('monday')));
            if (Date.today().compareTo(Date.parse('sunday')) >= 0) {
                $('#dateTo').val($.datepicker.formatDate('dd/mm/yy', Date.parse('next sunday')));
            }
            else {
                $('#dateTo').val($.datepicker.formatDate('dd/mm/yy', Date.parse('sunday')));
            }
            break;
        case 'Last Week':
            if (Date.today().compareTo(Date.parse('last monday')) > 0) {
                $('#dateFrom').val($.datepicker.formatDate('dd/mm/yy', Date.parse('last monday').addWeeks(-1)));
            }
            else {
                $('#dateFrom').val($.datepicker.formatDate('dd/mm/yy', Date.parse('last monday')));
            }
            $('#dateTo').val($.datepicker.formatDate('dd/mm/yy', Date.parse('last sunday')));
            break;
        case 'Last Month':
            $('#dateFrom').val($.datepicker.formatDate('dd/mm/yy', new Date(curDate.getFullYear(), curDate.getMonth() - 1, 1)));
            $('#dateTo').val($.datepicker.formatDate('dd/mm/yy', new Date(curDate.getFullYear(), curDate.getMonth(), 0)));
            break;
        case 'This Year':
            $('#dateFrom').val($.datepicker.formatDate('dd/mm/yy', new Date(curDate.getFullYear(), 0, 1)));
            $('#dateTo').val($.datepicker.formatDate('dd/mm/yy', new Date(curDate.getFullYear(), 12, 0)));
            break;
        case 'By Month':
            SetDateRange('This Month');
            $('#monthSliderContainer').show();
            break;
        case 'By Period':
            $("#dateFrom, #dateTo").datepicker('enable');
            break;
        case 'This Month':
        default:
            $('#dateFrom').val($.datepicker.formatDate('dd/mm/yy', Date.parse('1st day')));
            $('#dateTo').val($.datepicker.formatDate('dd/mm/yy', new Date(curDate.getFullYear(), curDate.getMonth() + 1, 0)));
            break;
    }
}

function onChecked(ctl, hiddenListId) {
    var list = $('#' + hiddenListId).val();
    if ($(ctl).is(':not(checked)')) {
        list += '|' + $(ctl).val() + '|';
    }
    else {
        list = list.replace('|' + $(ctl).val() + '|', '');
    }
    $('#' + hiddenListId).val(list);
}

function isDecimal(sText) {
    if (sText == "")
        return true;

    var ValidChars = "0123456789.,";    

    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }

    return IsNumber;
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}